package cn.youyeku.zsxh.ui.activity.tabAct;


import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.youyeku.zsxh.R;
import cn.youyeku.zsxh.api.XhApi;
import cn.youyeku.zsxh.app;
import cn.youyeku.zsxh.bean.Help;
import cn.youyeku.zsxh.ui.UIControl;
import cn.youyeku.zsxh.ui.activity.ABS.BaseActivity;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ActMutualHelp extends BaseActivity {

    @BindView(R.id.iv_accept_help_message_headimg)
    ImageView ivAcceptHelpMessageHeadimg;
    @BindView(R.id.tv_accept_help_message_nickname)
    TextView tvAcceptHelpMessageNickname;
    @BindView(R.id.tv_accept_help_message_time)
    TextView tvAcceptHelpMessageTime;
    @BindView(R.id.tv_accept_help_message_text)
    TextView tvAcceptHelpMessageText;
    @BindView(R.id.tv_accept_help_message_address)
    TextView tvAcceptHelpMessageAddress;
    @BindView(R.id.tv_accept_help_message_reward)
    TextView tvAcceptHelpMessageReward;
    private Help help;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_act_mutual_help);
        ButterKnife.bind(this);

        help = (Help) getIntent().getSerializableExtra("help");
        tvAcceptHelpMessageNickname.setText(help.getNick_name());
        tvAcceptHelpMessageTime.setText(help.getHelp_time());
        tvAcceptHelpMessageText.setText(help.getHelp_content());
        tvAcceptHelpMessageAddress.setText(help.getHelp_address());
        tvAcceptHelpMessageReward.setText(help.getHelp_reward());
        Glide.with(getApplicationContext())
                .load(XhApi.getFace(help.getHelp_author_uid()))
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .error(R.drawable.default_avatar)
                .into(ivAcceptHelpMessageHeadimg);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {


        menu.add(Menu.NONE, Menu.FIRST, Menu.NONE, "接受")
                .setEnabled(true)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);


        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case Menu.FIRST:
                XhApi.post_help_accept(help.getHelp_id(), new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        try {
                            JSONObject object = new JSONObject(response.body().string());
                            app.backgroundThreadShortToast(object.getString("message"));
                            int code = object.getInt("code");
                            if (code == 200) {
                                finish();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @OnClick(R.id.iv_accept_help_message_headimg)
    public void onClick() {
        UIControl.showUserInfo(getApplicationContext(), help.getHelp_author_uid());
    }
}
